package com.example.seekme;

import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity implements Runnable{
    /** Called when the activity is first created. */
    MediaPlayer mp= new MediaPlayer(); 
    Button playButton;
    SeekBar seekMe; 
    Button stopButton;
    boolean mStartPlaying ; 
    TextView totTemp;
    TextView currentTemp;
    
    

    int total;
    int currentPosition=0;
	// INDIRIZZO DEL FILE AUDIO!
    private static String mFileName =Environment.getExternalStorageDirectory().getAbsolutePath()+"/AccelAudio/audiorecordtest.3gp";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mStartPlaying= true;
    totTemp=(TextView)findViewById(R.id.totTemp);
    currentTemp=(TextView)findViewById(R.id.currentTemp);
    playButton = (Button)findViewById(R.id.play);
    stopButton =(Button)findViewById(R.id.stop);
    seekMe = (SeekBar)findViewById(R.id.seekBar1);

    try {
        mp.setDataSource(mFileName);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setLooping(true);
        mp.prepareAsync();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mp.setOnPreparedListener(new OnPreparedListener(){
        @Override
        public void onPrepared(MediaPlayer arg0) {
            // TODO Auto-generated method stub
            total = mp.getDuration();
        }

    });

    seekMe.setProgress(0);

    playButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
        	if(mStartPlaying){
        	playButton.setText("Pause");
            mp.start();
            seekMe.setMax(total);
            totTemp.setText(Integer.toString(total));
            
        	}else{
        		playButton.setText("Play");
        		mp.pause();
        	}
        mStartPlaying=!mStartPlaying;

        }

    });

    stopButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0){
            mp.reset();
            
            mStartPlaying=true;
            playButton.setText("Play");
            
            try {
                mp.setDataSource(mFileName);
                mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mp.setLooping(true);
                mp.prepareAsync();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    });

    seekMe.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub

            if(fromUser){
                mp.seekTo(progress);
                currentTemp.setText(Integer.toString(progress));
            }

        }
    });
    seekMe.setProgress(currentPosition);
    Thread currentThread = new Thread(this);
    currentThread.start();


}

@Override
public void run() {
    // TODO Auto-generated method stub

    try{
        while(mp != null){
             currentPosition = mp.getCurrentPosition();
            Message msg = new Message();
            msg.what = currentPosition;
            threadHandler.sendMessage(msg);
        }
        Thread.sleep(100);
    }catch (InterruptedException e){
        e.printStackTrace();
    }
}

private Handler threadHandler = new Handler(){
    public void handleMessage(Message msg){
        seekMe.setProgress(msg.what);
        currentTemp.setText(Integer.toString(msg.what));

    }
};
}
